package javaCode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * Created by lxy_920912 on 14/04/2017.
 */
public class LeetCode222 {
    public int countNodes(TreeNode root) {
        int leftDeep = getLeftDeep(root);
        int rightDeep =getRightDeep(root);
        if(leftDeep == rightDeep){

            return (1<<leftDeep) -1;

        }else {
            return 1 + countNodes(root.left) + countNodes(root.right);
        }
    }
    public int getLeftDeep(TreeNode root){
        if(root == null){
            return 0;
        }
        TreeNode p = root;
        int deep = 0;
        while(p != null){
            p = p.left;
            deep++;
        }
        return deep;
    }
    public int getRightDeep(TreeNode root){
        TreeNode p = root;
        int count = 0;
        while(p != null){
            p = p.right;
            count++;
        }
        return count;
    }

}
